﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace Forging2.Common
{
    public static class Const
    {
        
        /// <summary>
        /// 全局配置文件
        /// </summary>
        public static AppConfig Config;

        public static List<Heating.HeatingFurnace> HeatingFurnaces;
        public static List<bas_plcaddress_zy> HeatingPLCAddress ;
        public static Services.OPCService.OPCServer OPCServer ;
        //public static string Notification { get; set; }
        //public static string ErrorNotification { get; set; }
        //public static object Config { get; internal set; }

        public static DataBase.IGDataDbContext IGDataDbContext;
        public static DataBase.ZH_IMES.ZH_IMESDbContext ZH_IMESDbContext;

        public static Clamping.Clamping WorkClamping;
        public static Forging.ForgingDevice WorkForging;
        public static Rolling.RollingDevice WorkRolling;

        public static List<DataBase.ZH_IMES.V_ForgingOperateData> v_ForgingOperateDatas;
        public static List<DataBase.QID_IGDATA_ZY.V_WorkRecord> v_WorkRecords;

        //public static string ForgingState;

        public static FrontData Front =new FrontData();

        public static string CreateGID()
        {
            return Guid.NewGuid().ToString();
        }

        /// <summary>
        /// 数据库类型转换成使用类型
        /// </summary>
        /// <param name="sWP"></param>
        /// <param name="dWP"></param>
        /// <returns></returns>
        public static WorkingPiece WorkPieceDBToWorkpice(DataBase.ZH_IMES.V_ForgingOperateData sWP, WorkingPiece dWP)
        {
            if (sWP is null)
            {
                return null;
            }
            WorkingPiece workingPiece = new WorkingPiece()
            {
                GID = sWP.GID,
                WORK_CENTER_GID = sWP.WORK_CENTER_GID,
                DIS_CODE_STATE = sWP.DIS_CODE_STATE,
                PRODU_CODE = sWP.PRODU_CODE,
                PRODU_NAME = sWP.PRODU_NAME,
                LOT_CODE = sWP.LOT_CODE,
                ORDER_GID = sWP.ORDER_GID,
                MAKING_ORDER_CODE = sWP.MAKING_ORDER_CODE,
                WORK_CELL_GID = sWP.WORK_CELL_GID,
                WORKCELL_CODE = sWP.WORKCELL_CODE,
                WORKCELL_NAME = sWP.WORKCELL_NAME,
                InFurnace = sWP.InFurnace,
                ForgingNO = sWP.ForgingNO,
                WorkCenterCode = sWP.WorkCenterCode,
                WorkCenterName = sWP.WorkCenterName,
                RouteCode = sWP.RouteCode,
                RouteName = sWP.RouteName,
                MakingLotNo = sWP.MakingLotNo,
                MakingXS = sWP.MakingXS,
                PRODU_SIZE = sWP.PRODU_SIZE,
                ORDER_WEIGHT = sWP.ORDER_WEIGHT,
                PROCESS_WEIGHT = sWP.PROCESS_WEIGHT,
                PORCESS_COMBINE = sWP.PORCESS_COMBINE,
                ORDER_STATE = sWP.ORDER_STATE,
                WORK_ORDER_STATE = sWP.WORK_ORDER_STATE,
                PLAN_QTY = sWP.PLAN_QTY,
                ROUTE_GID = sWP.ROUTE_GID,
                PLANNED_START_TIME = sWP.PLANNED_START_TIME,
                PLANNED_FINISH_TIME = sWP.PLANNED_FINISH_TIME,
                ISCOMPLETE = sWP.ISCOMPLETE,
                PLAN_OP_SEQ = sWP.PLAN_OP_SEQ,
                CREATE_DATE = sWP.CREATE_DATE,
                CREATE_ID = sWP.CREATE_ID,
                MODIFY_DATE = sWP.MODIFY_DATE,
                MODIFY_ID = sWP.MODIFY_ID,
                FurnaceArea = sWP.FurnaceArea,
                InFurnaceTemperatures =dWP != null ? dWP.InFurnaceTemperatures:0,
                OutFurnaceTemperatures =dWP != null ? dWP.OutFurnaceTemperatures:0,
                WorkpieceHeight =dWP != null ? dWP.WorkpieceHeight:0,
                WorkpieceTemperatures =dWP !=null ? dWP.WorkpieceTemperatures:0,
                Operater=dWP!=null ?dWP.Operater:Const.Config.OperateCode,
            };
           
            return workingPiece;

        }
        /// <summary>
        /// 工件状态
        /// </summary>
        public enum WorkPieceState
        {
            /// <summary>
            /// 不明状态
            /// </summary>
            SXXX,
            /// <summary>
            /// 未在炉未开工
            /// </summary>
            S000,
            /// <summary>
            /// 未在炉已开工
            /// </summary>
            S001,
            /// <summary>
            /// 未在炉已暂停
            /// </summary>
            S002,
            /// <summary>
            /// 未在炉已完工
            /// </summary>
            S003,
            /// <summary>
            /// 在炉未开工
            /// </summary>
            S010,
            /// <summary>
            /// 在炉已开工
            /// </summary>
            S011,
            /// <summary>
            /// 在炉已暂停
            /// </summary>
            S012,
            /// <summary>
            /// 在炉已完工
            /// </summary>
            S013,
            /// <summary>
            /// 锻造未开工
            /// </summary>
            S100,
            /// <summary>
            /// 锻造已开工
            /// </summary>
            S101,
            /// <summary>
            /// 锻造已暂停
            /// </summary>
            S102,
            /// <summary>
            /// 锻造已完工
            /// </summary>
            S103,

        }
        /// <summary>
        /// 取料机抓取类型
        /// </summary>
        public enum ClampingType
        {
            HeatingFurnace,
            Forging,
            Rolling,
            Others
        }

        
        

        /// <summary>
        /// 前端数据绑定
        /// </summary>
        public class FrontData : DependencyObject
        {
            public static DependencyProperty NotificationProperty;
            public static DependencyProperty ErrorNotificationProperty;

            public static DependencyProperty ForgingCurrentHeightProperty;
            public static DependencyProperty ForgingBaseHeightProperty;
            public static DependencyProperty ForgingFixHeightProperty;
            public static DependencyProperty ForgingRealHeightProperty;
            public static DependencyProperty ForgingMinHeightProperty;
            public static DependencyProperty ForgingWorkpieceHeightProperty;
            public static DependencyProperty ForgingTemperatures1Property;
            public static DependencyProperty ForgingTemperatures2Property;
            public static DependencyProperty ForgingForgingSignalProperty;
            public static DependencyProperty ForgingZeroSignalProperty;

            static FrontData()
            {
                NotificationProperty = DependencyProperty.Register("Notification", typeof(string), typeof(FrontData), new PropertyMetadata("信息", OnValueChanged));
                ErrorNotificationProperty = DependencyProperty.Register("ErrorNotification", typeof(string), typeof(FrontData), new PropertyMetadata("错误", OnValueChanged));

                ForgingCurrentHeightProperty = DependencyProperty.Register("ForgingCurrentHeight", typeof(string), typeof(FrontData), new PropertyMetadata("0", OnForgingValueChanged));
                ForgingBaseHeightProperty = DependencyProperty.Register("ForgingBaseHeight", typeof(string), typeof(FrontData), new PropertyMetadata("0", OnForgingValueChanged));
                ForgingFixHeightProperty = DependencyProperty.Register("ForgingFixHeight", typeof(string), typeof(FrontData), new PropertyMetadata("0", OnForgingValueChanged));
                ForgingRealHeightProperty = DependencyProperty.Register("ForgingRealHeight", typeof(string), typeof(FrontData), new PropertyMetadata("0", OnForgingValueChanged));
                ForgingMinHeightProperty = DependencyProperty.Register("ForgingMinHeight", typeof(string), typeof(FrontData), new PropertyMetadata("0", OnForgingValueChanged));
                ForgingWorkpieceHeightProperty = DependencyProperty.Register("ForgingWorkpieceHeight", typeof(string), typeof(FrontData), new PropertyMetadata("0", OnForgingValueChanged));
                ForgingTemperatures1Property = DependencyProperty.Register("ForgingTemperatures1", typeof(string), typeof(FrontData), new PropertyMetadata("0", OnForgingValueChanged));
                ForgingTemperatures2Property = DependencyProperty.Register("ForgingTemperatures2", typeof(string), typeof(FrontData), new PropertyMetadata("0", OnForgingValueChanged));
                ForgingForgingSignalProperty = DependencyProperty.Register("ForgingForgingSingle", typeof(bool), typeof(FrontData), new PropertyMetadata(false, OnForgingValueChanged));
                ForgingZeroSignalProperty = DependencyProperty.Register("ForgingZeroSingle", typeof(bool), typeof(FrontData), new PropertyMetadata(false, OnForgingValueChanged));
            }


            private static void OnValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
            {
                ////throw new NotImplementedException();
            }

            private static void OnForgingValueChanged(DependencyObject d,DependencyPropertyChangedEventArgs e)
            {

            }
            public string Notification
            {
                get { return GetValue(NotificationProperty).ToString(); }
                //set { SetValue(NotificationProperty, value); }
                set
                {
                    App.Current.Dispatcher.Invoke(new Action(() =>
                    {
                        SetValue(NotificationProperty, DateTime.Now.ToLongTimeString() + "  " + value);
                        Console.WriteLine("提示信息： " + value);
                        AppLog.WriteInfoLog(value);
                    }));

                }
            }
            public string ErrorNotification
            {
                get { return GetValue(ErrorNotificationProperty).ToString(); }
                //set { SetValue(ErrorNotificationProperty, value); }
                set { App.Current.Dispatcher.Invoke(new Action(() => { SetValue(ErrorNotificationProperty, DateTime.Now.ToLongTimeString() + " " + value); })); }
            }


            public string ForgingCurrentHeight
            {
                get { return GetValue(ForgingCurrentHeightProperty).ToString(); }
                set { Application.Current.Dispatcher.Invoke(new Action(() => { SetValue(ForgingCurrentHeightProperty, value); })); }
            }
            public string ForgingBaseHeight
            {
                get { return GetValue(ForgingBaseHeightProperty).ToString(); }
                set { Application.Current.Dispatcher.Invoke(new Action(() => { SetValue(ForgingBaseHeightProperty, value); })); }
            }
            public string ForgingFixHeight
            {
                get { return GetValue(ForgingFixHeightProperty).ToString(); }
                set { Application.Current.Dispatcher.Invoke(new Action(()=> { SetValue(ForgingFixHeightProperty, value); })); }
            }
            public string ForgingRealHeight
            {
                get { return GetValue(ForgingRealHeightProperty).ToString(); }
                set { Application.Current.Dispatcher.Invoke(new Action(() => { SetValue(ForgingRealHeightProperty, value); })); }
            }
            public string ForgingMinHeight
            {
                get { return GetValue(ForgingMinHeightProperty).ToString(); }
                set { Application.Current.Dispatcher.Invoke(new Action(() => { SetValue(ForgingMinHeightProperty, value); })); }
            }
            public string ForgingWorkpieceHeight
            {
                get { return GetValue(ForgingWorkpieceHeightProperty).ToString(); }
                set { Application.Current.Dispatcher.Invoke(new Action(() => { SetValue(ForgingWorkpieceHeightProperty, value); })); }
            }
            public string ForgingTemperatures1
            {
                get => GetValue(ForgingTemperatures1Property).ToString();
                set => Application.Current.Dispatcher.Invoke(new Action(() => { SetValue(ForgingTemperatures1Property, value); }));
            }
            public string ForgingTemperatures2
            {
                get { return GetValue(ForgingTemperatures1Property).ToString(); }
                set { Application.Current.Dispatcher.Invoke(new Action(() => { SetValue(ForgingTemperatures2Property, value); })); }
            }
            public Boolean ForgingForgingSignal
            {
                get { return (bool)GetValue(ForgingForgingSignalProperty); }
                set { Application.Current.Dispatcher.Invoke(new Action(() => { SetValue(ForgingForgingSignalProperty, value); })); }
            }
            public bool ForgingZeroSignal
            {
                get { return (bool)GetValue(ForgingZeroSignalProperty); }
                set { Application.Current.Dispatcher.Invoke(new Action(() => { SetValue(ForgingZeroSignalProperty, value); })); }
            }
        }

    }
}
